package com.deliveryservicesystem.app.validation;

import com.jgoodies.validation.ValidationMessage;
import com.jgoodies.validation.ValidationResult;
import com.jgoodies.validation.view.ValidationResultViewFactory;
import java.util.List;
import javax.swing.JLabel;

/**
 * File Name: ValidationUtil.java
 * Version:
 * Description:
 * Package: com.deliveryservicesystem.app.validation
 * Author: Lance
 * Date: Jun 24, 2012
 * Editor:
 */
public class ValidationUtil
{

    public static void displayErrorsToLable(ValidationResult validationResult, JLabel lblValidationErrors)
    {

        List<ValidationMessage> errors = validationResult.getErrors();
        if (errors.isEmpty())
        {
            lblValidationErrors.setIcon(ValidationResultViewFactory.getCheckIcon());
            lblValidationErrors.setText("");
        }
        else
        {
            StringBuilder builder = new StringBuilder();
            builder.append("<html>");
            for (int i = 0; i < errors.size(); i++)
            {
                builder.append(errors.get(i).formattedText()).append("<br>");
            }
            builder.append("</html>");
            lblValidationErrors.setText(builder.toString());
            lblValidationErrors.setIcon(ValidationResultViewFactory.getErrorIcon());
            lblValidationErrors.setVisible(true);
        }
    }
}